const db = require('../db/index.js');
const express = require('express');
const router = express.Router();
const sql = 'CREATE DATABASE my_db_journalism';
const my_db_journalism = {
    users_sql : 'CREATE TABLE IF NOT EXISTS users(id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(45),password VARCHAR(45),status TINYINT(1))ENGINE=InnoDB DEFAULT CHARSET=utf8;',
    ev_users : 'CREATE TABLE IF NOT EXISTS ev_users(id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(255),password VARCHAR(255),nickname VARCHAR(255),email VARCHAR(255),user_pic TEXT)ENGINE=InnoDB DEFAULT CHARSET=utf8;',
    ev_article_cate_sql : 'CREATE TABLE IF NOT EXISTS ev_article_cate(Id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255),alias VARCHAR(255),is_delete TINYINT(1) NULL DEFAULT "0")ENGINE=InnoDB DEFAULT CHARSET=utf8;',
    ev_artices_sql : 'CREATE TABLE IF NOT EXISTS ev_artices(Id INT AUTO_INCREMENT PRIMARY KEY,title VARCHAR(255),content TEXT,cover_img TEXT,pub_date VARCHAR(255),state VARCHAR(255),is_delete TINYINT(1) NULL DEFAULT "0",cate_id INT,author_id INT)ENGINE=InnoDB DEFAULT CHARSET=utf8;'
};
const errey = '数据库初始化失败：解决方案-找到db目录下的index.js-找到database注释后刷新当前页-然后放开注释-数据库即可完成创建';
// TODO: 创建数据库
router.get( '/database',(req,res) => db.query(sql,() => my_db_one_users(res)) );
// TODO: 创建users表
const my_db_one_users = (res) => db.query( my_db_journalism['users_sql'],() => ev_users(res) );
// TODO: 创建ev_users表
function ev_users (res) { db.query(my_db_journalism['ev_users'],() => ev_article_cate(res)) };
// TODO: 创建ev_article_cate表
function ev_article_cate(res) { db.query(my_db_journalism['ev_article_cate_sql'],() => ev_artices(res)) };   
// TODO: 创建ev_artices表
function ev_artices(res) { db.query(my_db_journalism['ev_artices_sql'],(err) => { return err ?  res.send(errey) : res.send('数据库初始化成功,请勿覆盖操作') }) };
// 将创建数据库路由导出
module.exports = router;